/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skyolder.milkbee.mq;

import java.io.IOException;

import javax.inject.Inject;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Component;

@Component
public class Consumer {
	@Inject
	SimpMessageSendingOperations messagingTemplate;
	@JmsListener(destination = "milkbee.instant.queue")
	public void receiveQueue(String sinstant) {
	//	System.out.println(instant);\
		//ObjectMapper mapper = new ObjectMapper();
		//mapper.writeValueAsString(instant);
		ObjectMapper mapper = new ObjectMapper();  
		try {
			Instant instant;
			instant = mapper.readValue(sinstant, Instant.class);
			messagingTemplate.convertAndSendToUser(instant.getCode(), "/instant", instant.getMyMessage());
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}

}
